module.exports = class extends think.Model {
  async queryData(platformId, {page = 1, limit = 10, field = '*', username = ''}) {
    const where = {
      platform_id: platformId,
      is_deleted: global.IS_DELETED.no
    };
    if (username) {
      where.username = username;
    }
    const rows = await this.where(where).page(page, limit).field(field).select();
    const total = await this.where(where).count();
    return {
      rows,
      total
    };
  }
  async updateById(platformId, id, data) {
    return this.where({id, platform_id: platformId}).update(data);
  }
  queryAdminByUserName(platformId, username, {field = '*'}) {
    return this.where({platform_id: platformId, username, is_deleted: global.IS_DELETED.no}).field(field).find();
  }
  async queryAdminByUserNameAndPwd(platformId, username, pwd, {field = '*'}) {
    const admin = await this.queryAdminByUserName(platformId, username, {field});
    if (!think.isEmpty(admin)) {
      if (admin.pwd === think.md5(pwd)) {
        return admin;
      }
    }
    return null;
  }
  async removeById(platformId, id) {
    return this.where({
      id,
      platformId
    }).update({
      is_deleted: global.IS_DELETED.yes
    });
  }
  async updateStatus(platformId, id, status) {
    return this.where({
      platform_id: platformId,
      id
    }).update({
      status
    });
  }
};
